package article_service

import (
	"gitee.com/xuaxu/go-gin/models/article_new"
	"gitee.com/xuaxu/go-gin/models/kepu_res"
)

type Article struct {
	ID            int
	Title         string
	Desc          string
	Content       string
	CoverImageUrl string
	State         int
	CreatedBy     string
	ModifiedBy    string

	PageNum  int
	PageSize int
}

type ArticleNew struct {
	Title         string `json:"title"`
	Desc          string `json:"desc"`
	Content       string `json:"content"`
	CoverImageUrl string `json:"cover_image_url"`
	CreatedBy     string `json:"created_by"`
	ModifiedBy    string `json:"modified_by"`
	State         int    `json:"state"`
	Id            int    `json:"id"`
	Url           string `json:"url"`
}

//
//func (a *Article) Add() error {
//	article := map[string]interface{}{
//		"tag_id":          a.TagID,
//		"title":           a.Title,
//		"desc":            a.Desc,
//		"content":         a.Content,
//		"created_by":      a.CreatedBy,
//		"cover_image_url": a.CoverImageUrl,
//		"state":           a.State,
//	}
//
//	if err := models.AddArticle(article); err != nil {
//		return err
//	}
//
//	return nil
//}

//func (a *Article) Edit() error {
//	return models.EditArticle(a.ID, map[string]interface{}{
//		"tag_id":          a.TagID,
//		"title":           a.Title,
//		"desc":            a.Desc,
//		"content":         a.Content,
//		"cover_image_url": a.CoverImageUrl,
//		"state":           a.State,
//		"modified_by":     a.ModifiedBy,
//	})
//}

func (a *Article) ExistByID() (bool, error) {
	return article_new.ExistArticleByID(a.ID)
}

func (a *Article) Get(page int, size int) ([]ArticleNew, error, int) {
	artiles, _, count := article_new.Get(page, size)
	var articleNews []ArticleNew
	var article_uuids []string
	for _, article := range artiles {
		article_uuids = append(article_uuids, article.Uuid)
	}

	imgList := make(map[string]string)
	articleImgs, _ := kepu_res.Get(article_uuids)
	for _, img := range articleImgs {
		imgList[img.Uuid] = img.Url
	}
	//获取简介内容

	articleDetail, _ := kepu_res.GetDetail(article_uuids)

	for _, article := range artiles {
		articleNews = append(articleNews, ArticleNew{
			Id:            article.Id,
			Title:         article.Title,
			Url:           imgList[article.Uuid],
			CoverImageUrl: imgList[article.Uuid],
			Desc:          articleDetail[article.Uuid]["summary"],
			Content:       articleDetail[article.Uuid]["content"],
		})
	}

	return articleNews, nil, count
}

type List struct {
	Id    int
	Uuid  string
	Title string
}

func SyncData(page int, size int) (list []List, count int, err error) {
	arr := []string{}
	arr = append(arr, "0")
	res, count, _ := kepu_res.GetArticle(page, size)
	res_channel := make(chan int)
	res_channel2 := make(chan List)
	for _, resdetail := range res {
		go func(resdetail kepu_res.Res) {
			res_channel <- resdetail.Id
			list = append(list, List{Id: resdetail.Id, Uuid: resdetail.Uuid, Title: resdetail.Title})
		}(resdetail)
		<-res_channel
	}
	close(res_channel)
	for _, des := range res {
		res_channel2 <- List{Id: des.Id, Uuid: des.Uuid, Title: des.Title}
	}
	close(res_channel2)
	return list, count, nil
}
